package com.cy.web;

import com.alibaba.fastjson.JSONArray;
import com.cy.bean.Coach;
import com.cy.bean.Driverschool;
import com.cy.service.CoachService;
import com.cy.service.DriverService;
import com.cy.token.JwtUtil;
import com.cy.token.ResultResponse;
import com.cy.token.VoToken;
import com.cy.token.VoUserInfo;
import com.cy.util.CacheRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RestController
public class DriverController {


    @Autowired
    private DriverService driverService;


    @Autowired
    private CacheRedisService cacheRedisService;

    @PostMapping("/loginOut")
    public ResultResponse logout(@RequestHeader("token") String token){
        ResultResponse res = new ResultResponse();
        // 验证token的合法和有效性
        String tokenValue = JwtUtil.verity(token);// success:zhangsan1
        // 获取token中的用户名
        String username = tokenValue.replaceFirst(JwtUtil.TOKEN_SUCCESS, "");
        // 移除session中的登录标记（或者redis中的登录标记）
        res.setMessage("logout success");
        res.setData("logout success");
        res.setCode(20000);
        return res;
    }



    @GetMapping("/getInfo")
    public ResultResponse info(@RequestParam("token") String token){
        ResultResponse res = new ResultResponse();
        System.out.println(token+"我是");
        // 验证token的合法和有效性
        String tokenValue = JwtUtil.verity(token);// success:zhangsan1
        System.out.println(tokenValue+"我是token");
        if(tokenValue != null && tokenValue.startsWith(JwtUtil.TOKEN_SUCCESS)) {
            // 如果ok-》返回需要的用户信息
            // zhangsan1
            String username = tokenValue.replaceFirst(JwtUtil.TOKEN_SUCCESS, "");

            //下面的应该是从数据库中拿到的，现在写死了
//            VoUser user = this.userService.searchUserByUserName(username);
            VoUserInfo info = new VoUserInfo();
            info.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            info.setIntroduction("测试用户");
            info.setName(username);
            System.out.println(info);
            //给用户设定角色
            int rid= Integer.parseInt(cacheRedisService.get("rid"));
            List<String> roles;
            if (rid==2){
                roles = Arrays.asList("2");
            }else {
                roles = Arrays.asList("3");
            }
            info.setRoles(roles);

            res.setData(info);
            res.setMessage(JwtUtil.TOKEN_SUCCESS);
            res.setCode(20000);
        }else {
            // 否则：500
            res.setCode(20001);
            res.setMessage(JwtUtil.TOKEN_SUCCESS);
        }
        System.out.println(res+"我是res");
        return res;
    }

    @PostMapping("/loginAdmin")
    public ResultResponse login(@RequestBody Driverschool driverschool){
        ResultResponse res = new ResultResponse();

        try {
            //调用UserService完成username和password的验证
            Driverschool driverschool1=driverService.driverlogin(driverschool);
            //根据验证结果，组成响应对象返回
            if (driverschool1!=null){
                //创建一个token数据，封装到res对象中
                cacheRedisService.add("did", driverschool1.getDid());
                cacheRedisService.add("rid", driverschool1.getRid());
                String token = JwtUtil.sign(driverschool.getDacc(), "-1");
                res.setCode(20000);
                res.setMessage(JwtUtil.TOKEN_SUCCESS);
                res.setData(new VoToken(token));
            }else {
                res.setCode(20001);
                res.setMessage("用户名和密码不匹配");
                res.setData("fail");
            }
        } catch (Exception e) {
            res.setCode(20001);
            res.setMessage("用户名和密码不匹配");
            res.setData("fail");
            e.printStackTrace();
        }
        return res;
    }

}
